% The UNESCO equation: Chen and Millero (1977)
% t - temperature in °C
% p - pressure in mBar
% s - water salinity in PSU
function [speed_of_sound] = PHX_SpeedOfSound_Calc(t, p_mBar, s)

    C00 = 1402.388;
    C01 = 5.03830;
    C02 = -5.81090E-2;
    C03 = 3.3432E-4;
    C04 = -1.47797E-6;
    C05 = 3.1419E-9;
    C10 = 0.153563;
    C11 = 6.8999E-4;
    C12 = -8.1829E-6;
    C13 = 1.3632E-7;
    C14 = -6.1260E-10;
    C20 = 3.1260E-5;
    C21 = -1.7111E-6;
    C22 = 2.5986E-8;
    C23 = -2.5353E-10;
    C24 = 1.0415E-12;
    C30 = -9.7729E-9;
    C31 = 3.8513E-10;
    C32 = -2.3654E-12;
    A00 = 1.389;
    A01 = -1.262E-2;
    A02 = 7.166E-5;
    A03 = 2.008E-6;
    A04 = -3.21E-8;
    A10 = 9.4742E-5;
    A11 = -1.2583E-5;
    A12 = -6.4928E-8;
    A13 = 1.0515E-8;
    A14 = -2.0142E-10;
    A20 = -3.9064E-7;
    A21 = 9.1061E-9;
    A22 = -1.6009E-10;
    A23 = 7.994E-12;
    A30 = 1.100E-10;
    A31 = 6.651E-12;
    A32 = -3.391E-13;
    B00 = -1.922E-2;
    B01 = -4.42E-5;
    B10 = 7.3637E-5;
    B11 = 1.7950E-7;
    D00 = 1.727E-3;
    D10 = -7.9836E-6;

    t2 = t * t;
    t3 = t2 * t;
    t4 = t3 * t;
    p = p_mBar / 1000.0;

    Cw = (C00 + C01 * t + C02 * t2 + C03 * t3 + C04 * t4 + C05 * t4 * t) +...
               (C10 + C11 * t + C12 * t2 + C13 * t3 + C14 * t4) * p +...
               (C20 + C21 * t + C22 * t2 + C23 * t3 + C24 * t4) * p * p +...
               (C30 + C31 * t + C32 * t2) * p * p * p;

    A = (A00 + A01 * t + A02 * t2 + A03 * t3 + A04 * t4) +...
              (A10 + A11 * t + A12 * t2 + A13 * t3 + A14 * t4) * p +...
              (A20 + A21 * t + A22 * t2 + A23 * t3) * p * p +...
              (A30 + A31 * t + A32 * t2) * p * p * p;

    B = B00 + B01 * t + (B10 + B11 * t) * p;

    D = D00 + D10 * p;

    speed_of_sound = Cw + A * s + B * sqrt(s * s * s) + D * s * s;
end
